/**
 * Created by black on 2017/4/10.
 */

var express = require('express');
var _ = require('lodash');
var router = express.Router();
var certificationDao = require('../dao/aijiduo/certification-dao');
var vegetableDao = require('../dao/aijiduo/vegetable-dao');
var fruitDao = require('../dao/aijiduo/fruit-dao');
var i18n = require('i18n');

router.get('/', function (req, res) {
  certificationDao.getAllCertifications(function (certs) {
    res.render('aijiduo/index.ejs', {title: '爱吉多-首页'});
  });
});

router.get('/certification', function (req, res) {
  certificationDao.getAllActiveCertification(function (certs) {
    var locale = req.getLocale();
    console.log('locale', locale);
    for(var i = 0; i < certs.length; i ++) {
      switch (locale) {
        case 'cn' :
          certs[i].name = certs[i].cnName;
          break;
        case 'en' :
          certs[i].name = certs[i].enName;
          break;
        case 'es' :
          certs[i].name = certs[i].spaName;
          break;
      }
    }
    var certLists = _.chunk(certs, 4);
    var renderData = {
      certLists: certLists,
      originCerts: certs
    };
    
    res.render('aijiduo/certification.ejs', renderData);
  });
});
router.get('/vegetable/:season/:vegetableType/:pageNo', function (req, res) {
  let pageNo = req.params.pageNo;
  let season = req.params.season;
  let vegetableType = req.params.vegetableType;
  let pageObject = {
    season : season,
    pageNo : pageNo,
    totalPage : 0,
    type : vegetableType,
  };
  vegetableDao.getVegetablesByFilter({
    pageNo: pageNo,
    season: season,
    type: vegetableType
  }, function (vegetables, totalNumber) {
    pageObject.vegetables = vegetables || [];
    pageObject.totalNumber = totalNumber;
    res.render('aijiduo/vegetable.ejs', {po: pageObject});
  });
});
router.get('/fruit', function (req, res) {
  fruitDao.getAllActiveFruits(function (fruits) {
    console.log('fruits', fruits);
    var renderData = {
      fruits: fruits || []
    };
    res.render('aijiduo/fruit.ejs', renderData);
  });
  
});
router.get('/meat', function (req, res) {
  res.render('index.ejs', {title: '爱吉多-优质禽蛋'});
});
router.get('/marketing', function (req, res) {
  res.render('aijiduo/marketing.ejs', {title: '爱吉多-营销网络'});
});
router.get('/package', function (req, res) {
  res.render('index.ejs', {title: '爱吉多-会员宅配套餐'});
});
router.get('/preorder', function (req, res) {
  res.render('aijiduo/preorder.ejs', {title: '爱吉多-预约采摘'});
});
router.get('/gift-box', function (req, res) {
  res.render('aijiduo/gift-box.ejs', {title: '爱吉多-礼盒定制'});
});
router.get('/contact-us', function (req, res) {
    res.render('aijiduo/contact-us.ejs', {title: '爱吉多-联系我们'});
});

module.exports = router;